<template>
	<view class="login-container">

		<view class="wechatapp">
			<view class="header">
				<open-data style="width: 196rpx;height: 196rpx;" class="" type="userAvatarUrl"></open-data>
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle" v-if="btn_show == false">获得你的公开信息（昵称、头像等）</view>
		<view class="auth-subtitle" v-else>获得你的手机号（为了您的用户体验，请绑定您的手机号）</view>
		<view class="login-btn">
			<button v-if="btn_show == false" type="primary" class="btn-normal" data-eventsync="true" @tap="getUserInfo">授权信息</button>
			<button v-else class="btn-normal" open-type="getPhoneNumber" @getphonenumber="getPhone">绑定手机号</button>
		</view>
		<view class="no-login-btn" v-if="btn_show == false">
			<button class="btn-normal" @click="onNotLogin">暂不登录</button>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				background: '',
				listData: [],
				btn_show:false
			}
		},
		onShow() {
			//wx.login(); //重新登录
		},
		onLoad() {

		},
		methods: {
			/*改变发送验证码按钮文本*/
			changeMsg() {
				if (this.second > 0) {
					this.send_btn_txt = this.second + '秒';
					this.second--;
					setTimeout(this.changeMsg, 1000);
				} else {
					this.send_btn_txt = '获取验证码';
					this.second = 60;
					this.is_send = false;
				}
			},
			onNotLogin: function() {
				this.gotoPage('/pages/index/index')
			},
			//手机号
			getPhone(e) {
				let self = this;
				uni.login({
					success(res) {
						// 发送用户信息
						self._post('user.user/getSession', {
							code: res.code
						}, result => {
							self._post('user.user/bindMobile', {
								session_key:  result.data.session_key,
								iv: e.detail.iv,
								encrypted_data: e.detail.encryptedData,
							}, function(res) {
								console.log(res, 'res');
								uni.navigateBack();
							});
						});
					}
				});
			},
			getUserInfo() {
				let self = this;
				uni.getUserProfile({
					desc: '登录',
					success(res) {
						console.log('res', res);
						uni.setStorageSync('nickName', res.userInfo.nickName);
						uni.setStorageSync('avatarUrl', res.userInfo.avatarUrl);
						uni.login({
							success(res_login) {
								self._post('user.user/wxlogin', {
									code: res_login.code,
									user_info: res.rawData,
									iv: res.iv,
									signature: res.signature,
									encryptedData: res.encryptedData,
									source: 'wx'
								}, result => {
									uni.setStorageSync('code', res_login.code);
									uni.setStorageSync('token', result.data.token);
									uni.setStorageSync('user_id', result.data.user_id);
									self.getPhone()
									self.btn_show = true
									
								});
							}
						});
					},
					fail(res) {
						console.log("getUserProfile 调用失败", res);
					},
				});
			},
		},
	}
</script>

<style>
	.login-container {
		padding: 30rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #04be01;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #dfdfdf;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}
</style>